package dynaspring.xml;

import java.io.IOException;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;

import dynaspring.LispBeanDefinitionReader;

public class DynaspringImportParser implements BeanDefinitionParser {

	@Override
	public BeanDefinition parse(Element el, ParserContext ctx) {
		String resource = el.getAttribute("resource");
		if (!StringUtils.hasText(resource)) {
			ctx.getReaderContext().error("Resource location must not be empty", el);
			return null;
		}
		
		LispBeanDefinitionReader reader = new LispBeanDefinitionReader(ctx.getRegistry());
		try {
			reader.loadBeanDefinitions(ctx.getReaderContext().getResource(), resource);
		} catch (IOException e) {
			ctx.getReaderContext().error("Error loading resource " + resource, el, e);
		}
		return null;
	}

}
